package com.atom.app.lifecycle

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.lifecycle.viewModelScope
import com.atom.app.lifecycle.base.*
import com.atom.app.lifecycle.bean.Account
import com.atom.app.lifecycle.databinding.ActivitySplashBinding
import com.atom.module.logger.Logger
import com.atom.module.mvvm.adapter.ClickEvent
import com.atom.module.mvvm.livedata.SingleLiveData
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import kotlinx.coroutines.launch
import java.util.*


@SuppressLint("CustomSplashScreen")
class SplashActivity : AppActivity<ActivitySplashBinding, SplashViewModel>() {

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_splash
    }

    override fun initViewModel(): SplashViewModel {
        return providerViewModel(AppViewModelFactory, SplashViewModel::class)
    }

    override fun initVariableId(): Int {
        return BR.splashViewModel
    }

    override fun initData(view: View) {
        super.initData(view)
        viewModel.showDateDialog.observe(this) { callback ->
            val picker = DatePicker(this)
            val wheelLayout = picker.wheelLayout
            picker.setOnDatePickedListener { year, month, day ->
                viewModel.toast("$year-$month-$day")
                callback.invoke(year, month, day)
            }
            wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY)
            wheelLayout.setRange(DateEntity.today(), DateEntity.yearOnFuture(10))
            wheelLayout.setDateLabel("年", "月", "日")
            picker.show()
        }
    }
}

class SplashViewModel : AppViewModel<AppModel>(AppModel) {

    val showDateDialog = SingleLiveData<(Int, Int, Int) -> Unit>()

    val gotoMain = object : ClickEvent() {
        override fun invoke(t: View) {
            loadOrCreate()
        }
    }

    fun loadOrCreate() {
        val loadUser = model.loadUser()
        if (loadUser.isNotEmpty()) {
            viewModelScope.launch {
                val account = model.loadAccountByUUID(loadUser)
                Logger.e(1, account, Thread.currentThread())
                if (account != null) {
                    open(loadUser)
                    return@launch
                }
                model.resetUser()
                loadOrCreate()
            }
            return
        }
        // 创建新用户并 执行跳转操作
        showDateDialog.setValue { year, month, day ->
            // 创建一个新的用户
            val account = Account.Bean(year, month, day)
            // 入库
            viewModelScope.launch {
                val insert = model.insertAccount(account)
                val loadAccount = model.loadAccount(insert) ?: return@launch
                Logger.e(loadAccount, Thread.currentThread())
                // 进行初始化文件
                model.saveUser(loadAccount.uuid)
                // 进行跳转
                open(loadAccount.uuid)
            }
        }
    }

    fun open(loadUser: String) {
        startActivity(RecordActivity::class.java, Bundle().apply {
            this.putString(AppConfig.currentUser, loadUser)
        })
    }
}